import '../constants/app_constants.dart';

class Validator {
  static bool isEmail(String? value) {
    if (value == null || value.isEmpty) return false;
    return RegExp(AppConstants.emailRegex).hasMatch(value);
  }

  static bool isPhone(String? value) {
    if (value == null || value.isEmpty) return false;
    return RegExp(AppConstants.phoneRegex).hasMatch(value);
  }

  static bool isPassword(String? value) {
    if (value == null || value.isEmpty) return false;
    return RegExp(AppConstants.passwordRegex).hasMatch(value);
  }

  static bool isNotEmpty(String? value) {
    return value != null && value.trim().isNotEmpty;
  }

  static bool isLength(String? value, int min, [int? max]) {
    if (value == null) return false;
    if (value.length < min) return false;
    if (max != null && value.length > max) return false;
    return true;
  }

  static bool isNumeric(String? value) {
    if (value == null || value.isEmpty) return false;
    return double.tryParse(value) != null;
  }

  static bool isInteger(String? value) {
    if (value == null || value.isEmpty) return false;
    return int.tryParse(value) != null;
  }

  static bool isUrl(String? value) {
    if (value == null || value.isEmpty) return false;
    return Uri.tryParse(value) != null;
  }

  static bool isIdCard(String? value) {
    if (value == null || value.isEmpty) return false;
    const String idCardRegex = r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$';
    return RegExp(idCardRegex).hasMatch(value);
  }

  static bool isChineseName(String? value) {
    if (value == null || value.isEmpty) return false;
    const String chineseNameRegex = r'^[\u4e00-\u9fa5]{2,10}$';
    return RegExp(chineseNameRegex).hasMatch(value);
  }

  static bool isLicensePlate(String? value) {
    if (value == null || value.isEmpty) return false;
    const String licensePlateRegex = r'^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$';
    return RegExp(licensePlateRegex).hasMatch(value);
  }

  static bool isBankCard(String? value) {
    if (value == null || value.isEmpty) return false;
    const String bankCardRegex = r'^[1-9]\d{12,19}$';
    return RegExp(bankCardRegex).hasMatch(value);
  }

  static bool isIPAddress(String? value) {
    if (value == null || value.isEmpty) return false;
    const String ipRegex = r'^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$';
    return RegExp(ipRegex).hasMatch(value);
  }

  static bool isInRange(num? value, num min, num max) {
    if (value == null) return false;
    return value >= min && value <= max;
  }

  static bool isImageFile(String? fileName) {
    if (fileName == null || fileName.isEmpty) return false;
    final extension = fileName.split('.').last.toLowerCase();
    return AppConstants.allowedImageTypes.contains(extension);
  }

  static bool isValidFileSize(int? fileSize) {
    if (fileSize == null) return false;
    return fileSize <= AppConstants.maxFileSize;
  }

  static String? validateRequired(String? value, String fieldName) {
    if (!isNotEmpty(value)) {
      return '$fieldName不能为空';
    }
    return null;
  }

  static String? validateEmail(String? value) {
    if (!isNotEmpty(value)) {
      return '邮箱不能为空';
    }
    if (!isEmail(value)) {
      return '请输入正确的邮箱格式';
    }
    return null;
  }

  static String? validatePhone(String? value) {
    if (!isNotEmpty(value)) {
      return '手机号不能为空';
    }
    if (!isPhone(value)) {
      return '请输入正确的手机号格式';
    }
    return null;
  }

  static String? validatePassword(String? value) {
    if (!isNotEmpty(value)) {
      return '密码不能为空';
    }
    if (!isLength(value, 8, 20)) {
      return '密码长度必须在8-20位之间';
    }
    if (!isPassword(value)) {
      return '密码必须包含大小写字母和数字';
    }
    return null;
  }

  static String? validateConfirmPassword(String? value, String? password) {
    if (!isNotEmpty(value)) {
      return '确认密码不能为空';
    }
    if (value != password) {
      return '两次输入的密码不一致';
    }
    return null;
  }

  static String? validateLength(String? value, int min, int max, String fieldName) {
    if (!isNotEmpty(value)) {
      return '$fieldName不能为空';
    }
    if (!isLength(value, min, max)) {
      return '$fieldName长度必须在$min-$max位之间';
    }
    return null;
  }

  static String? validateNumeric(String? value, String fieldName) {
    if (!isNotEmpty(value)) {
      return '$fieldName不能为空';
    }
    if (!isNumeric(value)) {
      return '$fieldName必须是数字';
    }
    return null;
  }

  static String? validateRange(String? value, num min, num max, String fieldName) {
    if (!isNotEmpty(value)) {
      return '$fieldName不能为空';
    }
    if (!isNumeric(value)) {
      return '$fieldName必须是数字';
    }
    final numValue = double.parse(value!);
    if (!isInRange(numValue, min, max)) {
      return '$fieldName必须在$min-$max之间';
    }
    return null;
  }
}